package com.net.xpay.common.manager.impl.stat;

import com.google.common.base.Preconditions;
import com.net.xpay.common.manager.stat.UserAccountMoneyLeftStatManager;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.xpay.common.dao.stat.UserAccountMoneyLeftStatMapper;
import com.net.xpay.common.domain.stat.UserAccountMoneyLeftStat;
import com.net.common.manager.AbstractManager;
import com.net.xpay.common.model.search.UserAccountMoneyLeftStatSearchModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;


@Component
public class UserAccountMoneyLeftStatManagerImpl extends AbstractManager<UserAccountMoneyLeftStat> implements UserAccountMoneyLeftStatManager {

    @Autowired
    private UserAccountMoneyLeftStatMapper mapper;


    @Override
    protected BasicMapper<Long, UserAccountMoneyLeftStat> getMapper() {
        return mapper;
    }

    @Override
    public UserAccountMoneyLeftStat findByDayAndUserId(LocalDate day, Long userId) {
        Preconditions.checkArgument(day != null && userId != null);
        UserAccountMoneyLeftStat condition = new UserAccountMoneyLeftStat();
        condition.setDay(day);
        condition.setUserId(userId);
        return getByCondition(condition);
    }

    @Override
    public List<UserAccountMoneyLeftStat> listByDay(LocalDate day) {
        Preconditions.checkArgument(day != null);
        UserAccountMoneyLeftStat condition = new UserAccountMoneyLeftStat();
        condition.setDay(day);
        return listByCondition(condition, new ArrayList<>());
    }

    @Override
    public List<UserAccountMoneyLeftStat> search(UserAccountMoneyLeftStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("day", "desc");
        page.addOrder("user_id", "desc");
        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(UserAccountMoneyLeftStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public UserAccountMoneyLeftStat sumUserAccountMoneyLeftStat(UserAccountMoneyLeftStatSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.sumUserAccountMoneyLeftStat(searchModel);
    }
}
